<template>
  <div class="app-container">
    <div>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="竞赛">
          <el-row>
            <el-col :span="12">
              <el-input v-model="form.name"></el-input>
            </el-col>
            <el-col :span="12">
              <el-button type="primary" style="margin-left: 5px" @click="openContestsDialog=true">选择</el-button>
            </el-col>
          </el-row>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-row>
              <el-col :span="12">
                <el-form-item label="获奖日期" prop="awardDate">
                  <el-date-picker clearable
                                  v-model="form.awardDate"
                                  type="date"
                                  value-format="yyyy-MM-dd"
                                  placeholder="请选择获奖日期">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="获奖等级" prop="awardGrade">
                  <el-select v-model="form.awardGrade" placeholder="请选择获奖等级">
                    <el-option
                      v-for="dict in dict.type.award_grade"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注"/>
              </el-form-item>
            </el-row>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支撑材料" prop="attachmentUrl">
              <file-upload v-model="form.attachmentUrl" @fileInfo="handleFileInfo"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="指导教师" prop="teacher">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <!--<span>指导教师</span>-->
              <el-button style="float: right; padding: 3px 0" type="text" @click="remGuideTeachers">删除</el-button>
              <el-button style="float: right; padding: 3px 0;margin-right: 5px" type="text" @click="showTeacherDialog">
                添加
              </el-button>
            </div>
            <div>
              <el-table :data="guideTeacherList" @selection-change="multiSelGuideTeacher">
                <el-table-column type="selection" width="55" align="center"/>
                <el-table-column label="姓名" align="center" prop="personName" width="150"/>
                <el-table-column label="排序" align="center" width="75">
                  <template slot-scope="scope">
                    <el-input v-model.number="scope.row.orderNum"></el-input>
                  </template>
                </el-table-column>
                <el-table-column label="贡献度" align="center" width="75">
                  <template slot-scope="scope">
                    <el-input v-model.number="scope.row.conDegree"></el-input>
                  </template>
                </el-table-column>
                <el-table-column label="工作内容" align="center">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.workContent"></el-input>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-card>
        </el-form-item>
        <el-form-item label="参赛学生" prop="stus">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <el-button style="float: right; padding: 3px 0" type="text" @click="remContestStus">删除</el-button>
              <el-button style="float: right; padding: 3px 0;margin-right: 5px" type="text" @click="showStuDialog">添加
              </el-button>
            </div>
            <div>
              <el-table :data="contestStuList" @selection-change="multiSelContestStu">
                <el-table-column type="selection" width="55" align="center"/>
                <el-table-column label="姓名" align="center" prop="personName" width="150"/>
                <el-table-column label="排序" align="center" width="75">
                  <template slot-scope="scope">
                    <el-input v-model.number="scope.row.orderNum"></el-input>
                  </template>
                </el-table-column>
                <el-table-column label="贡献度" align="center" width="75">
                  <template slot-scope="scope">
                    <el-input v-model.number="scope.row.conDegree"></el-input>
                  </template>
                </el-table-column>
                <el-table-column label="工作内容" align="center">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.workContent"></el-input>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-card>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-popover
          placement="top-start"
          title="提示"
          width="200"
          trigger="hover"
          content="点击将提交并返回登记管理页面">
          <el-button type="primary" slot="reference" :disabled="disabled" @click="submitForm">添 加</el-button>
        </el-popover>
        <el-popover
          style="margin: 0px 4px 0px 4px"
          placement="top-start"
          title="提示"
          width="200"
          trigger="hover"
          content="除支撑材料外，其余数据保持不变，请再次选择支撑材料">
          <el-button type="success" slot="reference" @click="submitNext">添加并继续</el-button>
        </el-popover>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </div>

    <!--添加竞赛对话框-->
    <el-dialog title="选择竞赛" :visible.sync="openContestsDialog" fullscreen append-to-body>
      <el-form :model="queryContestParams" ref="queryForm" size="small" :inline="true" label-width="110px">
        <el-form-item label="教育部认可赛事" prop="inMinistry" >
          <el-select v-model="queryContestParams.inMinistry" style="width: 118px" clearable>
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="竞赛等次" prop="grade">
          <el-select v-model="queryContestParams.grade" placeholder="请选择竞赛等次" clearable>
            <el-option
              v-for="dict in dict.type.contest_grade"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="竞赛级别" prop="type">
          <el-select v-model="queryContestParams.rank" placeholder="请选择竞赛类别" clearable>
            <el-option
              v-for="dict in dict.type.sub_contest_rank"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="竞赛名称" prop="name">
          <el-input v-model="queryContestParams.name" placeholder="请输入竞赛名称"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getContestList()">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQueryContest">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="contestInfoList" highlight-current-row @current-change="handleCurrentChange"
                @row-click="rowClick" @cell-click="cellClick">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="竞赛名称">
                <span>{{ props.row.params.parentName }}</span>
              </el-form-item>
              <el-form-item label="子竞赛名称">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-row>
                <el-col :span="5">
                  <el-form-item label="竞赛时间">
                    <span>{{ props.row.competitionDate }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="竞赛通知网址">
                    <a :href="props.row.params.url"><span style="color: blue">{{ props.row.params.url }}</span></a>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="年度">
                    <span>{{ props.row.year }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="报名费用">
                    <span>{{ props.row.params.fee }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="竞赛频次">
                    <div>
                      <dict-tag :options="dict.type.contest_freq" :value="props.row.params.freq"/>
                    </div>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="竞赛简介">
                <div v-html="props.row.params.brief"></div>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="年份" align="center" prop="year" width="80"/>
        <el-table-column label="竞赛名称" align="center" prop="params.parentName"/>
        <el-table-column label="子竞赛名称" align="center" prop="name"/>
        <el-table-column label="教育部认可赛事" align="center" prop="params.inMinistry" width="112">
          <template slot-scope="scope">
            <a v-if="scope.row.params.inMinistry == 1 || scope.row.params.inMinistry == 'Y' || scope.row.params.inMinistry == 'y'">是</a>
            <a v-else-if="scope.row.params.inMinistry != 1|| scope.row.params.inMinistry == 'N' || scope.row.params.inMinistry == 'n'">否</a>
          </template>
        </el-table-column>
        <el-table-column label="竞赛等次" align="center" prop="rank" width="80">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.contest_grade" :value="scope.row.params.grade"/>
          </template>
        </el-table-column>
        <el-table-column label="竞赛级别" align="center" prop="grade" width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sub_contest_rank" :value="scope.row.rank"/>
          </template>
        </el-table-column>
        <el-table-column label="竞赛时间" align="center" prop="competitionDate" width="100"/>
      </el-table>
      <pagination
        v-show="contestTotal>0"
        :total="contestTotal"
        :page.sync="queryContestParams.pageNum"
        :limit.sync="queryContestParams.pageSize"
        @pagination="getContestList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addContest">确 定</el-button>
        <el-button @click="cancelAddContest">取 消</el-button>
      </div>
    </el-dialog>

    <!--添加指导教师对话框-->
    <el-dialog title="添加指导教师" :visible.sync="openTeacherDialog" fullscreen append-to-body>
      <el-table :data="teacherList" @selection-change="multiSelTeacher">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-row>
                <el-col :span="5">
                  <el-form-item label="姓名">
                    <span>{{ props.row.personName }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="性别">
                    <div>
                      <dict-tag :options="dict.type.sys_user_sex" :value="props.row.gender"/>
                    </div>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="所在部门">
                    <span>{{ props.row.params.deptName }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="职称">
                    <div>
                      <dict-tag :options="dict.type.teacher_pro" :value="props.row.professional"/>
                    </div>
                  </el-form-item>
                </el-col>
                <el-col :span="5">
                  <el-form-item label="职务">
                    <div>
                      <dict-tag :options="dict.type.teacher_post" :value="props.row.post"/>
                    </div>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="研究方向">
                <span>{{ props.row.research }}</span>
              </el-form-item>
              <el-form-item label="简介">
                <div v-html="props.row.brief"></div>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="姓名" align="center" prop="personName"/>
        <el-table-column label="性别" align="center" prop="gender">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
          </template>
        </el-table-column>
        <el-table-column label="职称" align="center" prop="professional">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.teacher_pro" :value="scope.row.professional"/>
          </template>
        </el-table-column>
        <el-table-column label="职务" align="center" prop="post">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.teacher_post" :value="scope.row.post"/>
          </template>
        </el-table-column>
        <el-table-column label="研究方向" align="center" prop="research"/>
      </el-table>
      <pagination
        v-show="teacherTotal>0"
        :total="teacherTotal"
        :page.sync="queryTeacherParams.pageNum"
        :limit.sync="queryTeacherParams.pageSize"
        @pagination="getTeacherList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addTeacher">确 定</el-button>
        <el-button @click="cancelAddTeacher">取 消</el-button>
      </div>
    </el-dialog>

    <!--添加参赛学生对话框-->
    <el-dialog title="添加参赛学生" :visible.sync="openStuDialog" fullscreen append-to-body>
      <el-form :model="queryStuParams" size="mini" :inline="true">
        <el-form-item label="学号" prop="stuNo">
          <el-input
            v-model="queryStuParams.stuNo"
            placeholder="请输入学号"
            clearable
            @keyup.enter.native="getStuList"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getStuList">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQueryStu">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="stuList" @selection-change="multiSelStu">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-row>
                <el-col :span="8">
                  <el-form-item label="学号">
                    <span>{{ props.row.stuNo }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="姓名">
                    <span>{{ props.row.personName }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="性别">
                    <div>
                      <dict-tag :options="dict.type.sys_user_sex" :value="props.row.sex"/>
                    </div>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="班级">
                    <span>{{schoolAreaStr(props.row.schoolArea)}}-{{ props.row.params.collegeName }}-{{ props.row.params.grade }}-{{ props.row.params.proClass }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="身份证号">
                    <span>{{ props.row.idNum }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="宿舍">
                    <span>{{schoolAreaStr(props.row.schoolArea)}}-{{ props.row.params.buildingNo }}-{{ props.row.params.roomNo }}-{{ props.row.params.bedNo }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="手机号">
                    <span>{{ props.row.phone }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="学号" align="center" prop="stuNo" width="130"/>
        <el-table-column label="姓名" align="center" prop="personName" width="70"/>
        <el-table-column label="性别" align="center">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
          </template>
        </el-table-column>
        <el-table-column label="班级" align="center">
          <template slot-scope="scope">
            <span>{{schoolAreaStr(scope.row.schoolArea)}}-{{ scope.row.params.college }}-{{ scope.row.params.grade }}-{{ scope.row.params.proClass }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="stuTotal>0"
        :total="stuTotal"
        :page.sync="queryStuParams.pageNum"
        :limit.sync="queryStuParams.pageSize"
        @pagination="getStuList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addStu">确 定</el-button>
        <el-button @click="cancelAddStu">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listWithParentContest} from "@/api/contest/subContest";
  import {addAwardByVo, updateAwardBySaveAwardVo, awardDetail} from "@/api/contest/award";
  import {listWithDeptName, detailWithDeptName} from "@/api/contest/teacher";
  import {listStu, getStu} from "@/api/contest/stu";
  import UploadfileItem from "@/components/UploadfileItem";


  export default {
    name: "AddAward",
    dicts: ['sys_normal_disable', 'sys_yes_no','award_grade', 'contest_grade', 'sub_contest_rank', 'contest_in_ministry', 'teacher_post', 'sys_user_sex', 'teacher_pro', 'contest_freq'],
    data() {
      return {
        form: {},
        nextForm:{
          awardDate:null,
          awardGrade:"",
          contest: null,
          contestId: null,
          name: null,
          subContestId: null
        },
        awardId: null,
        // 表单校验
        rules: {
          // 子竞赛名称
          name: [
            {required: true, message: "必须选择一项竞赛", trigger: "blur"}
          ],
        },
        openContestsDialog: false,
        openTeacherDialog: false,
        openStuDialog: false,
        contestInfoList: [],
        teacherList: [],
        stuList: [],
        contestStuList: [],
        selectedTeacherList: [],
        remGuideTeacherList: [],
        selectedStuList: [],
        remContestStuList: [],
        guideTeacherList: [],
        contestTotal: 0,
        teacherTotal: 0,
        stuTotal: 0,
        orderTeacherNum: 1,
        orderStuNum: 1,
        contestNames: [],
        subContestIds: [],
        contestIds: [],
        queryContestParams: {
          pageSize: 10
        },
        queryTeacherParams: {
          pageSize: 10
        },
        queryStuParams: {
          pageNum: 1,
          pageSize: 10,
        },
        currentRow: null,
        disabled: false,

        fileList: [],
      }
    },
    components: {
      UploadfileItem
    },
    activated() {
      this.form = {}
    },
    created() {
      let awardId = this.$route.params.awardId;
      if (awardId && awardId !== 0 && awardId !== '0') {
        this.awardId = awardId
        this.getAwardDetail(awardId)
      }
      this.getContestList()
      this.getTeacherList()
      this.getStuList()
    },
    methods: {
      handleFileInfo(val) {
        this.fileList = val
      },
      cancelFile(file) {
        console.log(file);
      },
      getAwardDetail(awardId) {
        awardDetail(awardId).then(res => {
          this.form = res.data;
          this.form.name = res.data.subContestName;
          this.guideTeacherList = res.data.guideTeacherList
          this.guideTeacherList.map(item => {
            item.teacherId = item.personId
          })
          this.contestStuList = res.data.contestStuList
          this.contestStuList.map(item => {
            item.id = item.personId
          })
        })
      },
      schoolAreaStr(schoolAreaId) {
        if (schoolAreaId === 1) return '新校区'
        if (schoolAreaId === 2) return '老校区'
      },
      submitForm() {
        this.form.awardId = this.awardId
        this.form.guideTeacherList = this.guideTeacherList
        let teacherCondeTotal=0;
        let stuCondeTotal=0;
        this.form.guideTeacherList.forEach(item=>{
          teacherCondeTotal += item.conDegree
        })
        this.form.contestStuList = this.contestStuList
        this.form.contestStuList.forEach(item=>{
          stuCondeTotal += item.conDegree
        })
        if (this.fileList.length === 1) {
          this.form.originalFilename = this.fileList[0].originalFilename || ''
          this.form.accessUrl = this.fileList[0].accessUrl || ''
        } else if (this.fileList.length === 2) {
          this.form.originalFilename = this.fileList[1].originalFilename || ''
          this.form.accessUrl = this.fileList[1].accessUrl || ''
        }
        if (!this.form.name) return this.$message.error('必须选择一项竞赛')
        if (teacherCondeTotal!==100) return this.$message.error('指导教师贡献度之和应为100')
        if (stuCondeTotal!==100) return this.$message.error('参赛学生贡献度之和应为100')
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.disabled = true
            if (this.form.awardId) {
              updateAwardBySaveAwardVo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.disabled = false
              });
            } else {
              addAwardByVo(this.form).then(response => {
                this.nextForm.name = this.form.name
                this.nextForm.awardDate = this.form.awardDate
                this.nextForm.awardGrade = this.form.awardGrade
                this.nextForm.contest = this.form.contest
                this.nextForm.contestId = this.form.contestId
                this.nextForm.subContestId = this.form.subContestId
                this.$modal.msgSuccess("新增成功");
                this.$tab.openPage("获奖登记", "/contest/award")
                this.disabled =false
              });
            }
            const obj = { path: "/contest/add-award/index/"+this.awardId, name: "" };
            this.$tab.closePage(obj).then(() => {
              // 执行结束的逻辑
              this.$tab.openPage("获奖登记", "/contest/award");
            });
          }
        });
      },
      getContestList() {
        listWithParentContest(this.queryContestParams).then(res => {
          this.contestInfoList = res.rows;
          this.contestTotal = res.total;
        });
      },
      getTeacherList() {
        listWithDeptName().then(res => {
          this.teacherList = res.rows;
          this.teacherList = this.arrDif(this.teacherList, this.guideTeacherList)
          this.teacherList.map(item => {
            item.personName = item.name
          })
          this.teacherTotal = res.total;
        });
      },
      getStuList() {
        listStu(this.queryStuParams).then(res => {
          this.stuList = res.data.rows;
          this.stuList.forEach(item => {
            item.sex = item.sex + ''
          })
          this.stuList = this.arrDif2(this.stuList, this.contestStuList)
          this.stuList.map(item => {
            item.personName = item.name
          })
          this.stuTotal = res.data.total;
        });
      },
      addContest() {
        this.form.contest = this.contestNames.toString()
        this.queryContestParams = {}
        this.openContestsDialog = false
      },
      addTeacher() {
        this.selectedTeacherList.forEach(item => {
          const teacherObj = {
            teacherId: item.teacherId,
            personId: item.teacherId,
            personName: item.name,
            brief: item.brief,
            params: item.params,
            orderNum: this.orderTeacherNum++,
            conDegree: 100,
            workContent: '',
            gender: item.gender,
            professional: item.professional,
            post: item.post,
            research: item.research,
          }
          this.guideTeacherList.push(teacherObj)
        })
        this.openTeacherDialog = false
      },
      addStu() {
        this.selectedStuList.forEach(item => {
          const stuObj = {
            id: item.id,
            personId: item.id,
            personName: item.name,
            params: item.params,
            phone: item.phone,
            schoolArea: item.schoolArea,
            stuNo: item.stuNo,
            sex: item.sex,
            idNum: item.idNum,
            orderNum: this.orderStuNum++,
            conDegree: 100,
            workContent: '',
          }
          this.contestStuList.push(stuObj)
        })
        this.openStuDialog = false
      },
      arrDif(arr1, arr2) {
        for (let i = 0; i < arr1.length; i++) {
          for (let j = 0; j < arr2.length; j++) {
            if (arr1.length > 0) {
              if (arr1[i].teacherId === arr2[j].teacherId) {
                arr1 = arr1.filter(item => item.teacherId !== arr1[i].teacherId)
              }
            }
          }
        }
        return arr1
      },
      arrDif2(arr1, arr2) {
        for (let i = 0; i < arr1.length; i++) {
          for (let j = 0; j < arr2.length; j++) {
            if (arr1[i].id === arr2[j].id && arr1.length > 0) {
              arr1 = arr1.filter(item => item.id !== arr1[i].id)
            }
          }
        }
        return arr1
      },
      showTeacherDialog() {
        this.teacherList = this.arrDif(this.teacherList, this.guideTeacherList)
        this.openTeacherDialog = true
      },
      showStuDialog() {
        this.stuList = this.arrDif2(this.stuList, this.contestStuList)
        this.openStuDialog = true
      },
      cancelAddContest() {
        this.queryContestParams = {}
        this.openContestsDialog = false
      },
      cancelAddTeacher() {
        this.openTeacherDialog = false
      },
      cancelAddStu() {
        this.queryStuParams = {}
        this.openStuDialog = false
      },
      popTeacher(teacher) {
        let tOrderNum = 1
        this.guideTeacherList.forEach(item => {
          if (item.teacherId !== teacher.teacherId) {
            item.orderNum = tOrderNum
            tOrderNum++
          }
        })
        this.guideTeacherList = this.guideTeacherList.filter(item => item.teacherId !== teacher.teacherId)
        let exist = false
        this.teacherList.forEach(item => {
          if (item.teacherId === teacher.teacherId) {
            exist = true
          }
        })
        if (!exist) {
          detailWithDeptName(teacher.teacherId).then(res => {
            let teacher = res.data
            teacher.personName = teacher.name
            this.teacherList.push(teacher)
            this.orderTeacherNum--
            if (this.orderTeacherNum === 0) {
              this.orderTeacherNum = 1
            }
          })
        }
      },
      popStu(stu) {
        let tOrderNum = 1
        this.contestStuList.forEach(item => {
          if (item.id !== stu.id) {
            item.orderNum = tOrderNum
            tOrderNum++
          }
        })
        this.contestStuList = this.contestStuList.filter(item => item.id !== stu.id)
        let exist = false
        this.stuList.forEach(item => {
          if (item.id === stu.id) {
            exist = true
          }
        })
        if (!exist) {
          getStu(stu.id).then(res => {
            let stu = res.data.data
            stu.personName = stu.name
            this.stuList.push(stu)
            this.orderStuNum--
            if (this.orderStuNum === 0) {
              this.orderStuNum = 1
            }
          })
        }
      },
      multiSelContest(selection) {
        this.contestNames = selection.map(item => item.name)
        this.subContestIds = selection.map(item => item.subContestId)
        this.contestIds = selection.map(item => item.contestId)
      },
      multiSelTeacher(selection) {
        this.selectedTeacherList = selection
      },
      multiSelGuideTeacher(selection) {
        this.remGuideTeacherList = selection
      },
      multiSelContestStu(selection) {
        this.remContestStuList = selection
      },
      remGuideTeachers() {
        this.remGuideTeacherList.forEach(item => {
          this.popTeacher(item)
        })
      },
      remContestStus() {
        this.remContestStuList.forEach(item => {
          this.popStu(item)
        })
      },
      multiSelStu(selection) {
        this.selectedStuList = selection
      },
      resetQueryStu() {
        this.queryStuParams = {}
        this.getStuList()
      },
      resetQueryContest() {
        this.queryContestParams = {}
        this.getContestList()
      },
      cancel() {
        this.form = {}
        this.$tab.openPage("获奖登记", "/contest/award");
      },
      handleCurrentChange(val) {
        this.currentRow = val;
        this.form.contestId = val.contestId
        this.form.subContestId = val.subContestId
        this.form.name = val.name
      },
      rowClick(row, column, event) {
      },
      cellClick(row, column, cell, event) {
        this.handleCurrentChange(row)
      },
      //添加并继续按钮
      submitNext(){
        this.form.awardId = this.awardId
        this.form.guideTeacherList = this.guideTeacherList
        let teacherCondeTotal=0;
        let stuCondeTotal=0;
        this.form.guideTeacherList.forEach(item=>{
          teacherCondeTotal += item.conDegree
        })
        this.form.contestStuList = this.contestStuList
        this.form.contestStuList.forEach(item=>{
          stuCondeTotal += item.conDegree
        })
        if (this.fileList.length === 1) {
          this.form.originalFilename = this.fileList[0].originalFilename || ''
          this.form.accessUrl = this.fileList[0].accessUrl || ''
        } else if (this.fileList.length === 2) {
          this.form.originalFilename = this.fileList[1].originalFilename || ''
          this.form.accessUrl = this.fileList[1].accessUrl || ''
        }
        if (!this.form.name) return this.$message.error('必须选择一项竞赛')
        if (teacherCondeTotal!==100) return this.$message.error('指导教师贡献度之和应为100')
        if (stuCondeTotal!==100) return this.$message.error('参赛学生贡献度之和应为100')
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.disabled = true
            if (this.form.awardId) {
              updateAwardBySaveAwardVo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.disabled = false
              });
            } else {
              addAwardByVo(this.form).then(response => {
                this.nextForm.name = this.form.name
                this.nextForm.awardDate = this.form.awardDate
                this.nextForm.awardGrade = this.form.awardGrade
                this.nextForm.contest = this.form.contest
                this.nextForm.contestId = this.form.contestId
                this.nextForm.subContestId = this.form.subContestId
                this.$modal.msgSuccess("新增成功");
                this.disabled =false
              });
            }
          }
        });

        this.$set(this.form.name,0,this.nextForm.name)

        // this.form.name = this.nextForm.name
        // this.form.awardDate = this.nextForm.awardDate
        // this.form.awardGrade = this.nextForm.awardGrade
        // this.form.contest = this.nextForm.contest
        // this.form.contestId = this.nextForm.contestId
        // this.form.subContestId = this.nextForm.subContestId
      },
    }
  }
</script>

<style scoped>
  .text {
    font-size: 14px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }

  .clearfix:after {
    clear: both
  }

  .demo-table-expand {
    font-size: 0;
  }

  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }

  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 100%;
  }

</style>
